﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModBot.ConfigBuilder.Providers;

namespace ModBot.ConfigBuilder.Forms
{
    public partial class DebuffInstanceForm : Form
    {
        public enum DebuffGemOption
        {
            alt,
            item,
            script
        }

        public Boolean IsCancelled { get; set; }
        public DebuffSettingInstanceProvider DebuffSettingInstanceProvider { get; set; }
        
        public DebuffInstanceForm()
        {
            InitializeComponent();
            this.IsCancelled = true;

            if (Globals.SpellDatabaseProvider != null && Globals.SpellDatabaseProvider.Spells != null)
            {
                debuffInstanceSpellCombobox.DataSource = Globals.SpellDatabaseProvider.Spells;
            }

            debuffInstanceTargetTypeCombobox.DataSource = Enum.GetNames(typeof(DebuffTargetType));

            debuffInstanceGemCombobox.Items.Add("");
            foreach(string name in Enum.GetNames(typeof(DebuffGemOption)))
            {
                debuffInstanceGemCombobox.Items.Add(name);
            }
        }

        public void LoadDebuffInstance(DebuffSettingInstanceProvider provider)
        {
            if (provider.Index <= 0) throw new InvalidOperationException("Attempt to load provider without first setting Index value.");
            this.DebuffSettingInstanceProvider = provider;

            LoadContent();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveContent();
        }

        private void LoadContent()
        {
            try
            {
                debuffInstanceGemCombobox.Text = DebuffSettingInstanceProvider.ADGem;
                debuffInstanceSpellCombobox.Text = DebuffSettingInstanceProvider.ADSpell;
                debuffInstanceSpellFociTextbox.Text = DebuffSettingInstanceProvider.ADSpellFoci.ToString();
                debuffInstanceDurationModTextbox.Text = DebuffSettingInstanceProvider.ADDurMod.ToString("00");
                debuffInstanceMinManaTextbox.Text = DebuffSettingInstanceProvider.ADSpellMinMana.ToString();
                debuffInstanceAliasTextbox.Text = DebuffSettingInstanceProvider.ADSpellAlias;
                debuffInstanceAnnounceTextbox.Text = DebuffSettingInstanceProvider.ADAnnouce.ToString();
                debuffInstanceTargetCountTextbox.Text = DebuffSettingInstanceProvider.ADTarCnt.ToString();

                debuffInstanceCastOnResistTextbox.Text = DebuffSettingInstanceProvider.ADSpellCastonResist;
                debuffInstanceTargetTypeCombobox.Text = Enum.GetName(typeof(DebuffTargetType), DebuffSettingInstanceProvider.ADTarType);
                debuffInstanceBegHpTextbox.Text = DebuffSettingInstanceProvider.ADTarBegHP.ToString();
                debuffInstanceEndHpTextbox.Text = DebuffSettingInstanceProvider.ADTarEndHP.ToString();
                debuffInstanceRecastTextbox.Text = DebuffSettingInstanceProvider.ADSpellRecast.ToString();
                debuffInstanceImmuneStepTextbox.Text = DebuffSettingInstanceProvider.ADIfSpellImmune;
                debuffInstanceDelayTextbox.Text = DebuffSettingInstanceProvider.ADSpellDelay.ToString();

                debuffInstancePreConditionCheckbox.Checked = DebuffSettingInstanceProvider.ADPreCondition;
                debuffInstanceUseHOTTCheckbox.Checked = (DebuffSettingInstanceProvider.ADUseHOTT != 0);

                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading debuff instance object. \r\n\r\n " + ex.Message, "General load error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SaveContent()
        {
            try
            {
                DebuffSettingInstanceProvider.ADGem = debuffInstanceGemCombobox.Text;
                DebuffSettingInstanceProvider.ADSpell=debuffInstanceSpellCombobox.Text;
                DebuffSettingInstanceProvider.ADSpellFoci =  debuffInstanceSpellFociTextbox.Text;
                DebuffSettingInstanceProvider.ADDurMod = Convert.ToInt32(debuffInstanceDurationModTextbox.Text);
                DebuffSettingInstanceProvider.ADSpellMinMana = Convert.ToInt32(debuffInstanceMinManaTextbox.Text);
                DebuffSettingInstanceProvider.ADSpellAlias = debuffInstanceAliasTextbox.Text;
                DebuffSettingInstanceProvider.ADAnnouce = debuffInstanceAnnounceTextbox.Text;
                DebuffSettingInstanceProvider.ADTarCnt = Convert.ToInt32(debuffInstanceTargetCountTextbox.Text);

                DebuffSettingInstanceProvider.ADSpellCastonResist = debuffInstanceCastOnResistTextbox.Text;
                DebuffSettingInstanceProvider.ADTarType = (DebuffTargetType)Enum.Parse(typeof(DebuffTargetType), debuffInstanceTargetTypeCombobox.Text);
                DebuffSettingInstanceProvider.ADTarBegHP = Convert.ToInt32(debuffInstanceBegHpTextbox.Text);
                DebuffSettingInstanceProvider.ADTarEndHP = Convert.ToInt32(debuffInstanceEndHpTextbox.Text);
                DebuffSettingInstanceProvider.ADSpellRecast = Convert.ToInt32(debuffInstanceRecastTextbox.Text);
                DebuffSettingInstanceProvider.ADIfSpellImmune = debuffInstanceImmuneStepTextbox.Text;
                DebuffSettingInstanceProvider.ADSpellDelay = Convert.ToInt32(debuffInstanceDelayTextbox.Text);
                DebuffSettingInstanceProvider.ADPreCondition = debuffInstancePreConditionCheckbox.Checked;
                DebuffSettingInstanceProvider.ADUseHOTT = debuffInstanceUseHOTTCheckbox.Checked ? 1 : 0;
                
                this.IsCancelled = false;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving debuff instance object. \r\n\r\n " + ex.Message, "General save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.IsCancelled = true;
            this.Close();
        }
    }
}
